<?php

namespace app\common\service\device;

use app\common\model\broadcast\DeviceRod;
use app\common\model\haikang\HkConsumerMsg;
use app\common\model\haikang\HkConsumerMsgFd;
use app\common\model\haikang\HkConsumerMsgWeek;
use app\common\model\work_area\HkDeviceSerial;
use app\common\model\work_area\WorkAreaStand;
use app\common\model\work_area\WorkAreaStandVoice;
use app\common\service\broadcast\IpBroadcastService;

/**
 * 雷达相关服务
 */
class RadarService
{
    /**
     * 写入雷达抓拍数据
     * @param $param
     * @return void
     */
    public static function dataPush($param)
    {
        $newString = str_replace('/www/wwwroot/ai_screen/public/', "", $param['path']);
        $path_arr = explode('/', $newString);
        if (count($path_arr) == 5) {
            //摄像头编号
            $device_serial = $path_arr[1];
            //摄像头通道号
            $channel_no = $path_arr[2];
            $hk_device_serial = HkDeviceSerial::alias('a')
                ->join("work_area_stand b ", " b.id = a.work_area_stand_id", 'left')
                ->join("work_area c ", " c.id = b.work_area_id", 'left')
                ->where(['device_serial' => $device_serial, 'channel_no' => $channel_no])
                ->field(['a.id', 'a.device_rod_id', 'a.work_area_stand_id', 'a.name', 'b.name bname', 'c.name st_line'])
                ->find();
            //找到摄像头
            if (!empty($hk_device_serial)) {
                $time = time();
                $data = [
                    'device_rod_id' => $hk_device_serial->device_rod_id,
                    'device_rod_name' => DeviceRod::where(['id' => $hk_device_serial->device_rod_id])->value('name'),
                    'work_area_stand_id' => $hk_device_serial->work_area_stand_id,
                    'platform_type' => 3,
                    'msg_id' => $path_arr[4],
                    'yj_data_id' => $path_arr[4],
                    'msg_type' => 'ai_message_task_result',
                    'msg_name' => "雷达侦测",
                    'channel_no' => $channel_no,
                    'device_serial' => $device_serial,
                    'device_name' => $hk_device_serial->name,
                    'device_serial_id' => $hk_device_serial->id,
                    'date_time' => $time,
                    'timestamp' => $time,
                    'image_url' => get_file_url($newString),
                    'server_img_url' => get_file_url($newString),
                    'st_line' => $hk_device_serial->st_line,
                    'cname' => $hk_device_serial->bname,
                    'play_url' => null
                ];
                HkConsumerMsg::create($data);
                HkConsumerMsgWeek::create($data);
                $voice_file = WorkAreaStandVoice::where(['hk_device_serial_id' => $hk_device_serial->id, 'event_type' => '雷达侦测'])
                    ->order("id DESC")
                    ->cache(60)
                    ->value('file');
                //远程调用ip广播
                if (!empty($voice_file)) {
                    //语音文件服务器域名
                    $play_url = 'http://ai.admin.lgsid.com/' . $voice_file;
                    //远程调用ip广播
                    IpBroadcastService::speech([
                        'url' => $play_url,
                        'hk_device_serial_id' => $hk_device_serial->id
                    ]);
                    $data['play_url'] = $play_url;
                }
                //获取站点信息远程调用led屏幕
                $work_area_stand = WorkAreaStand::where(['id' => $hk_device_serial->work_area_stand_id])->find();
                if (!empty($work_area_stand)) {
                    // LedService::send($work_area_stand->toArray());
                }
                //图片推送到大屏
                $hk_consumer_msg_fd = HkConsumerMsgFd::whereOr(['type' => 1])->whereOr(['work_area_stand_id' => 1])->column('fd');
                if (!empty($hk_consumer_msg_fd)) {
                    $url = "https://aisocket.zyckj.club/consumerMsgPush";
                    json_post($url, ['fd' => $hk_consumer_msg_fd, 'data' => $data]);
                }
                file_put_contents('return.txt', print_r($data, true));
                unset($voice_file);
                unset($data);
            }
        }
    }
}